package com.confinderence;

import com.confinderence.WebServiceTask.IResponseHandler;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ProfileActivity extends Activity {
	private EditText mPassword;
	private EditText mConfirm;
	private EditText mEmail;
	private Button mChangePass;
	private Button mChangeEmail;
	private Button mDeactivate;
	private WebServiceTask mTask;
	private static final String CHANGE_PASS_URL = Utils.REST_URL + "/lecturer/changePasswordMobile";
	private static final String CHANGE_EMAIL_URL = Utils.REST_URL + "/lecturer/changeEmailMobile";
	private static final String DEACTIVATE_URL = Utils.REST_URL + "/lecturer/deactivateMobile";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_profile);
		init();
	}

	private void init() {
		mPassword = (EditText) findViewById(R.id.profileChangePass);
		mConfirm = (EditText) findViewById(R.id.profileChangePassConfirm);
		mEmail = (EditText) findViewById(R.id.profileChangeEmail);

		mChangePass = (Button) findViewById(R.id.profileChangePassBtn);
		mChangeEmail = (Button) findViewById(R.id.profileChangeEmailBtn);
		mDeactivate = (Button) findViewById(R.id.profileDeactivateProfileBtn);

		mChangePass.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				changePass();
			}
		});
		mChangeEmail.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				changeEmail();
			}
		});
		mDeactivate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AlertDialog alertDialog = new AlertDialog.Builder(ProfileActivity.this)
						.setMessage(R.string.sure_message)
						.setTitle(R.string.sure_title)
						.setPositiveButton(R.string.positive,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog, int which) {
										deactivate();
									}
								})
						.setNegativeButton(R.string.negative,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog, int which) {
										dialog.dismiss();
									}
								}).create();
				alertDialog.show();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.logged_in_menu, menu);
		Utils.initMenu(menu, this);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	private void deactivate() {
		mTask = new WebServiceTask(WebServiceTask.POST_TASK, this, getString(R.string.wait_msg),
				new IResponseHandler() {

					@Override
					public void handleResponse(String response) {
						Utils.logout(ProfileActivity.this);
					}
				});
		mTask.addNameValuePair("username", Utils.getLecturer().getUsername());
		mTask.execute(DEACTIVATE_URL);
	}
	private void changeEmail() {
		mTask = new WebServiceTask(WebServiceTask.POST_TASK, this, getString(R.string.wait_msg),
				new IResponseHandler() {

					@Override
					public void handleResponse(String response) {
						Utils.goToHome(ProfileActivity.this);
					}
				});

		String newEmail = mEmail.getText().toString();
		mTask.addNameValuePair("username", Utils.getLecturer().getUsername());
		mTask.addNameValuePair("email", newEmail);
		mTask.execute(CHANGE_EMAIL_URL);
	}

	private void changePass() {

		String pass = mPassword.getText().toString();
		String confirmPass = mConfirm.getText().toString();
		if (pass.equals(confirmPass) && pass.length() != 0) {
			mTask = new WebServiceTask(WebServiceTask.POST_TASK, this,
					getString(R.string.wait_msg), new IResponseHandler() {

						@Override
						public void handleResponse(String response) {
							Utils.goToHome(ProfileActivity.this);
						}
					});
			mTask.addNameValuePair("username", Utils.getLecturer().getUsername());
			mTask.addNameValuePair("password", pass);
			mTask.execute(CHANGE_PASS_URL);
		} else {
			Toast.makeText(this, R.string.passwords_dont_match, Toast.LENGTH_LONG).show();
		}
	}
}
